package com.ruoyi.business.controller;


import com.ruoyi.business.domain.THomeworkStu;
import com.ruoyi.business.domain.bo.HomeWorkStuBo;
import com.ruoyi.business.domain.vo.*;
import com.ruoyi.business.service.ICourseInfoService;
import com.ruoyi.business.service.IHomeworkService;
import com.ruoyi.business.service.ITHomeworkStuService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Api("用户操作的controller")
@RestController
@RequestMapping("/business/homework")
public class HomeWorkController extends BaseController {

    @Autowired
    private IHomeworkService homeWorkService;

    @Autowired
    private ITHomeworkStuService homeworkStuService;

    @Autowired
    private ICourseInfoService courseInfoService;

    private final Logger logger = LoggerFactory.getLogger(HomeWorkController.class);


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        List<SysUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = null;
        return AjaxResult.success(message);
    }

    @GetMapping("/list")
    public TableDataInfo getHomeWorkList(HomeWorkStuBo bo) {
        startPage();

        String userId = getUserId();
        bo.setTeacherId(userId);
        List<HomeWorkStuVo> userList = homeWorkService.getUserList(bo);
        return getDataTable(userList);
    }


    @ApiOperation("查看本人课程作业")
    @GetMapping("/queryHomeWorkList")
    public AjaxResult queryHomeWorkList() throws Exception {
        String userId = SecurityUtils.getUserId();

        THomeworkStu bo = new THomeworkStu();
        bo.setUploadUser(String.valueOf(userId));
        List<THomeWorkStuVo> list = homeworkStuService.selectHomeworkStuVoList(bo);


        List<THomeWorkStuVo> notYetList = list.stream().filter(e -> e.getFinished().equals("0")).collect(Collectors.toList());
        List<THomeWorkStuVo> downList = list.stream().filter(e -> e.getFinished().equals("1")).collect(Collectors.toList());

        HomeWorkListVo listVo = new HomeWorkListVo();

        listVo.setDownList(downList);
        listVo.setNotYetList(notYetList);


        return AjaxResult.success(listVo);


    }
}
